<?php

/**
 * uniBoard ACP Edit Crawler
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_edit_crawler{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Our api
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	/**
	 * Available Styles
	 *
	 * @var array
	 */
	
	private $styles			= array();
	
	/**
	 * Available Languages
	 *
	 * @var array
	 */
	
	private $languages		= array();
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Create API
		$this -> api = new api_crawler( $appCore, $appCore -> _REQ( 'crawler'));
		
		// Crawler Loaded?
		if ( $this -> api -> data_loaded)
		{
			// Load Styles and Languages
			$this -> styles = $appCore -> getStyles( false);
			$this -> languages = $appCore -> getLanguages();
				
			// Update?
			if ( $appCore -> _POST( 'update') == 1)
			{
				// Fill Api
				$this -> api -> setCrawlerName( $appCore -> _POST( 'crawler_name'));
				$this -> api -> setCrawlerAgent( $appCore -> _POST( 'crawler_agent'));
				$this -> api -> setCrawlerStyle( $appCore -> _POST( 'crawler_style'));
				$this -> api -> setCrawlerLanguage( $appCore -> _POST( 'crawler_language'));
				
				// Style incorrect?
				if ( !isset( $this -> styles[$appCore -> _POST( 'crawler_style')]))
				{
					$this -> api -> setError( $appCore -> lang['acp_crawler_style_incorrect']);
				}
				
				// Language incorrect?
				if ( !isset( $this -> languages[$appCore -> _POST( 'crawler_language')]))
				{
					$this -> api -> setError( $appCore -> lang['acp_crawler_language_incorrect']);
				}
				
				// Api All-rite?
				if ( $this -> api -> error)
				{
					// Draw Errors
					$appCore -> output -> error( join( '<br />', $this -> api -> errors), $appCore -> lang['edit_crawler_form']);
					
					// Repeat Form
					$this -> drawForm();
				}
				else
				{
					// Save Api
					$this -> api -> saveData();
					
					// Set Log
					$appCore -> logAdmin( 'log_admin_edited_crawler', htmlspecialchars( $appCore -> _POST( 'crawler_name')));
					
					// Draw Message
					$appCore -> output -> message( $appCore -> lang['acp_crawler_edited'], htmlspecialchars( $this -> api -> crawler_name));
					
					// Jump back to Crawlers List
					new action_crawlers( $appCore);
				}
			}
			else
			{
				// Draw Form
				$this -> drawForm();
			}
		}
		else
		{
			// Draw Error
			$appCore -> output -> error( $appCore -> lang['acp_crawler_not_found'], $appCore -> lang['edit_crawler_form']);
			
			// Jump back to Crawlers List
			new action_crawlers( $appCore);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Form
	 *
	 */
	
	function drawForm(){
	
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
				
		// Cache output
		ob_start();
				
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_crawler&crawler=' . $this -> api -> crawler_id));
		echo $this -> appCore -> drawHidden( 'update', 1);
		echo $this -> appCore -> output -> openFormBlock();
		
		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_crawler_name'], $this-> appCore -> drawTextInput( 'crawler_name', $this -> api -> crawler_name));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_crawler_agent'], $this-> appCore -> drawTextInput( 'crawler_agent', $this -> api -> crawler_agent), $this -> appCore -> lang['acp_crawler_agent_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_crawler_style'], $this-> appCore -> drawList( 'crawler_style', $this -> styles, $this -> api -> crawler_style), $this -> appCore -> lang['acp_crawler_style_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_crawler_language'], $this-> appCore -> drawList( 'crawler_language', $this -> languages, $this -> api -> crawler_language), $this -> appCore -> lang['acp_crawler_language_expl']);
		
		// Close Form
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['save_changes']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['edit_crawler_form'], array(
			$this -> appCore -> adminLink( 'act=crawlers') => $this -> appCore -> lang['acp_web_crawlers'],
			'' => $this -> appCore -> lang['edit_crawler_form']
		), ob_get_clean());
			
	}

}